1066E - Binary Numbers AND Sum - CodeForces Solution


data structures implementation math *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define ff first
#define ss second
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define ins insert
#define endl "\n"
#define deb(x) cout<<(x)<<endl
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x<<" "<<x<<endl

const int INF=1e9;
const int MOD=998244353;
const int MAXN=1e5+5;


int power(int x, int y, int p)
{
 
    int res = 1;

	x = x % p;
 
	if(x == 0) return 0;
 
    while (y > 0) {
 
        if (y&1)
            res = (res * x) % p;
 
        y = y >> 1;

        x = (x * x) % p;
    }
    return res % p;
}



int tt=1,n,m;
string s1,s2;
void solve(){
	cin>>n>>m;
	cin>>s1>>s2;
	
	reverse(all(s1));
	reverse(all(s2));
	
	
	int pref[n];
	
	for(int i=0;i<n;i++) {
		if(s1[i] == '1') {
			
			if(i == 0) {
				pref[i] = power(2,i,MOD);
			} else {
				pref[i] = (pref[i-1] + power(2,i,MOD) ) % MOD;
			}
			
		}else {
			if(i == 0) pref[i] = 0;
			else pref[i] = pref[i-1];
		}
	}
	
	
	int ans = 0;
	
	
	for(int i=0;i<m;i++) {
		
		
		if(s2[i] == '1') ans = (ans + pref[min(n-1,i)]) % MOD;
		
	} deb(ans % MOD);

	
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
   // cin>>tt;
    while(tt--){
        solve();
    }
}



Comments

Submit
0 Comments
More Questions

1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs